﻿using System;
using System.Collections.Generic;

public interface IShape
{
    double CalculateArea();
    bool IsValid();
}

public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

public class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override double CalculateArea()
    {
        return IsValid() ? Length * Width : 0;
    }

    public override bool IsValid()
    {
        return Length > 0 && Width > 0;
    }
}

public class Square : Rectangle
{
    public Square(double side) : base(side, side) { }
}

public class Triangle : Shape
{
    public double SideA { get; set; }
    public double SideB { get; set; }
    public double SideC { get; set; }

    public Triangle(double sideA, double sideB, double sideC)
    {
        SideA = sideA;
        SideB = sideB;
        SideC = sideC;
    }

    public override double CalculateArea()
    {
        if (!IsValid())
            return 0;

        double s = (SideA + SideB + SideC) / 2;
        return Math.Sqrt(s * (s - SideA) * (s - SideB) * (s - SideC));
    }

    public override bool IsValid()
    {
        return SideA + SideB > SideC && SideA + SideC > SideB && SideB + SideC > SideA;
    }
}

public static class ShapeFactory
{
    private static readonly Random Random = new Random();

    public static Shape CreateRandomShape()
    {
        int shapeType = Random.Next(1, 4);
        double sideA, sideB, sideC;

        switch (shapeType)
        {
            case 1: 
                sideA = Random.Next(1, 11);
                sideB = Random.Next(1, 11);
                return new Rectangle(sideA, sideB);

            case 2: 
                sideA = Random.Next(1, 11);
                return new Square(sideA);

            case 3:
                do
                {
                    sideA = Random.Next(1, 11);
                    sideB = Random.Next(1, 11);
                    sideC = Random.Next(1, 11);
                } while (!(sideA + sideB > sideC && sideA + sideC > sideB && sideB + sideC > sideA));
                return new Triangle(sideA, sideB, sideC);

            default:
                throw new InvalidOperationException("无效的形状类型");
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        const int numberOfShapes = 10;
        List<Shape> shapes = new List<Shape>();

        for (int i = 0; i < numberOfShapes; i++)
        {
            shapes.Add(ShapeFactory.CreateRandomShape());
        }

        double totalArea = 0;
        foreach (Shape shape in shapes)
        {
            totalArea += shape.CalculateArea();
        }

        Console.WriteLine($"随机生成的{numberOfShapes}个形状的面积之和: {totalArea}");
        Console.Read();
    }
}